#include "MainWindow.h"
#include "ui_MainWindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("xPL Viewer");
    setWindowIcon(QIcon(":/images/eye.png"));
    modelMsgList = new MessageListWidget(this);
    ui->scrollArea->setStyleSheet("background: white;");
    ui->scrollArea->setWidget(modelMsgList);
    setStyleSheet(getStyleSheet());
    setupToolBar();
    setupMenu();
    service = new ViewerService(this);
    service->moveToThread(&thread);
    connect(service, SIGNAL(messageReceived(QxPLMessage)), this, SLOT(addMessage(QxPLMessage)));
    connect(this, SIGNAL(startService()), service, SLOT(listen()));
    connect(this, SIGNAL(stopService()), service, SLOT(close()));
    connect(&thread, SIGNAL(finished()), service, SLOT(deleteLater()));
    connect(this, SIGNAL(messageToSend(QxPLMessage)), service, SLOT(sendMessage(QxPLMessage)));
}

MainWindow::~MainWindow()
{
    thread.quit();
    thread.wait();
    delete ui;
}

void MainWindow::setupMenu() {
    connect(ui->actionQuit, SIGNAL(triggered()), qApp, SLOT(quit()));
    connect(ui->actionClear, SIGNAL(triggered()), this, SLOT(onActionClearTriggered()));
    connect(ui->actionFilter, SIGNAL(triggered()), this, SLOT(onActionFilterTriggered()));
    connect(ui->actionSendMessage, SIGNAL(triggered()), this, SLOT(onActionSendMessageTriggered()));
    connect(ui->actionSettings, SIGNAL(triggered()), this, SLOT(onActionSettingsTriggered()));
    ui->actionConnect->setCheckable(true);
    connect(ui->actionConnect, SIGNAL(triggered(bool)), this, SLOT(onActionConnectTriggered(bool)));
}

void MainWindow::onActionSettingsTriggered()
{
    qDebug() << "display settings...";
}

void MainWindow::onActionConnectTriggered(bool check)
{
    if(check) { //connection
        emit startService();
        ui->actionConnect->setIcon(QIcon(":/images/stop.png"));
        ui->actionConnect->setToolTip("Disconnect");
    }
    else { //disconnection
        emit stopService();
        ui->actionConnect->setIcon(QIcon(":/images/play.png"));
        ui->actionConnect->setToolTip("Connect to xPL Network");
    }
}

void MainWindow::setupToolBar() {
    ui->actionConnect->setIcon(QIcon(":/images/play.png"));
    ui->actionConnect->setToolTip("Connect to xPL Network");
    ui->mainToolBar->addAction(ui->actionConnect);

    ui->actionClear->setIcon(QIcon(":/images/trash.png"));
    ui->actionClear->setToolTip("Clear");
    ui->mainToolBar->addAction(ui->actionClear);

    ui->actionFilter->setIcon(QIcon(":/images/filter.png"));
    ui->actionFilter->setToolTip("Filter");
    ui->mainToolBar->addAction(ui->actionFilter);

    ui->actionSendMessage->setIcon(QIcon(":/images/upload.png"));
    ui->actionSendMessage->setToolTip("Send message");
    ui->mainToolBar->addAction(ui->actionSendMessage);
}

void MainWindow::onActionClearTriggered() {
    this->modelMsgList->clear();
}

void MainWindow::onActionFilterTriggered() {
    QStringList list;
    foreach(const QxPLFilter& filter, service->filters()) {
        list.append(filter.toString());
    }
    FilterDialog* filterDialog = new FilterDialog(list, this);
    filterDialog->show();
}

void MainWindow::onActionSendMessageTriggered() {
    //MessageCreationDialog* dialog = new MessageCreationDialog("./data/xpl_templates.xml");
    SendMessageDialog* dialog = new SendMessageDialog();
    connect(dialog, SIGNAL(messageCreated(QxPLMessage)), this, SIGNAL(messageToSend(QxPLMessage)));
    dialog->show();
}

void MainWindow::paintEvent(QPaintEvent *)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

QString MainWindow::getStyleSheet() {
    QString styleSheet = "";
    styleSheet +=
            ".MessageListWidget {"
            "   padding: 10px;"
            "   border-style: solid;"
            "   border-color: black;"
            "   border-width: 1px;"
            "   background: white;"
            "}"
            ".MessageWidget {"
            "   border-style: solid;"
            "   border-color: black;"
            "   border-width: 2px;"
            "}";
    return styleSheet;
}

void MainWindow::addMessage(QxPLMessage message)
{
    qDebug() << "add message";
    modelMsgList->add(message);
    ui->scrollArea->verticalScrollBar()->setSliderPosition(modelMsgList->height());
}
